/*LICENSE_BEGIN
 * ******************************************************************
 * All original parts of SoccerScope2003 have copyright (2003) by
 * Team YowAI, represented by Mr. Shuhei Shiota
 * The Department of Creative Informatics
 * The Graduate School of Information Science and Technology
 * The University of Tokyo
 * 
 * Additional classes (except libraries in /lib) were developed by 
 * the Dainamite Robocup-Team of the DAI-Labor, TU-Berlin 
 * (www.dai-labor.de)
 * 
 * Extended/new classes/packages are:
 *   soccerscope.analyzer.LogfileAnalyzer
 *   soccerscope.daistatistic.*
 *   soccerscope.daistatistic.charts.*
 *   soccerscope.daistatistic.tools.*
 *   soccerscope.db.*
 *   soccerscope.dialog.*
 *   soccerscope.model.PlayerWorldModel
 *   soccerscope.model.SimplePlayer
 *   soccerscope.net.AgentConnector
 *   soccerscope.training.*
 *   soccerscope.training.conditions.*
 * ******************************************************************
LICENSE_END*/
package soccerscope.view;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.FlowLayout;
import java.awt.GridBagConstraints;
import java.awt.GridBagLayout;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.awt.event.ItemEvent;
import java.awt.event.ItemListener;
import java.util.prefs.Preferences;

import javax.swing.Box;
import javax.swing.JButton;
import javax.swing.JCheckBox;
import javax.swing.JColorChooser;
import javax.swing.JComboBox;
import javax.swing.JDialog;
import javax.swing.JLabel;
import javax.swing.JPanel;
import javax.swing.JTabbedPane;
import javax.swing.JTextField;

import soccerscope.SoccerScope;
import soccerscope.model.ColorDB;
import soccerscope.model.GameEvent;
import soccerscope.model.SoccerScopePreferences;
import soccerscope.util.Color2;

public class PreferenceDialog implements ActionListener, ItemListener {

	private SoccerScope soccerscope;

	private JDialog jd;

	private JButton ok;

	private JButton cancel;

	/* GameEvent */
	private JCheckBox event[];

	private JTextField eventOffset[];

	/* Color */
	private JComboBox ballColor;

	private BallIcon ballIcon;

	private JComboBox leftPlayerColor;

	private PlayerIcon leftPlayerIcon;

	private JComboBox leftGoalieColor;

	private PlayerIcon leftGoalieIcon;

	private JComboBox rightPlayerColor;

	private PlayerIcon rightPlayerIcon;

	private JComboBox rightGoalieColor;

	private PlayerIcon rightGoalieIcon;

	/* Publish */
	private JComboBox typeBox;

	public PreferenceDialog(SoccerScope soccerscope) {
		this.soccerscope = soccerscope;

		this.jd = new JDialog(soccerscope, "Preference", true);

		JTabbedPane tabbedPane = new JTabbedPane(JTabbedPane.TOP,
				JTabbedPane.SCROLL_TAB_LAYOUT);
		this.ok = new JButton("OK");
		this.ok.addActionListener(this);
		this.cancel = new JButton("Cancel");
		this.cancel.addActionListener(this);
		JPanel jp = new JPanel();
		jp.setLayout(new FlowLayout(FlowLayout.RIGHT));
		jp.add(Box.createHorizontalStrut(200));
		jp.add(this.ok);
		jp.add(this.cancel);
		this.jd.getContentPane().setLayout(new BorderLayout());
		this.jd.getContentPane().add(tabbedPane, BorderLayout.CENTER);
		this.jd.getContentPane().add(jp, BorderLayout.SOUTH);

		/* GameEvent Tab */
		this.event = new JCheckBox[GameEvent.MAX];
		this.eventOffset = new JTextField[GameEvent.MAX];
		for (int i = GameEvent.MIN; i < GameEvent.MAX; i++) {
			this.event[i] = new JCheckBox(GameEvent.getEventName(i));
			this.eventOffset[i] = new JTextField("0", 4);
		}

		JPanel jp1 = new JPanel();
		jp1.setLayout(new GridBagLayout());
		GridBagConstraints gbc1 = new GridBagConstraints();
		GridBagConstraints gbc2 = new GridBagConstraints();
		gbc1.gridx = 0;
		gbc1.gridy = GridBagConstraints.RELATIVE;
		gbc1.ipadx = 5;
		gbc1.ipady = 5;
		gbc1.fill = GridBagConstraints.HORIZONTAL;
		gbc1.anchor = GridBagConstraints.NORTHWEST;

		gbc2.gridx = 1;
		gbc2.gridy = GridBagConstraints.RELATIVE;
		gbc2.ipadx = 5;
		gbc2.ipady = 5;
		gbc2.fill = GridBagConstraints.HORIZONTAL;
		gbc2.anchor = GridBagConstraints.NORTHWEST;

		jp1.add(new JLabel("EventType"), gbc1);
		jp1.add(new JLabel("play offset"), gbc2);

		for (int i = GameEvent.MIN; i < GameEvent.MAX; i++) {
			jp1.add(this.event[i], gbc1);
			jp1.add(this.eventOffset[i], gbc2);
		}

		tabbedPane.add("GameEvent", jp1);

		/* Color */
		String colors[] = { "black", "blue", "cyan", "darkGray", "gray",
				"green", "lightGray", "magenta", "orange", "pink", "red",
				"white", "yellow", "gold", "purple", "snow", "custom" };
		this.ballColor = new JComboBox(colors);
		this.ballIcon = new BallIcon(ColorDB.getColor("ball"));
		this.leftPlayerColor = new JComboBox(colors);
		this.leftPlayerIcon = new PlayerIcon(ColorDB.getColor("team_l_color"));
		this.leftGoalieColor = new JComboBox(colors);
		this.leftGoalieIcon = new PlayerIcon(ColorDB.getColor("goalie_l_color"));
		this.rightPlayerColor = new JComboBox(colors);
		this.rightPlayerIcon = new PlayerIcon(ColorDB.getColor("team_r_color"));
		this.rightGoalieColor = new JComboBox(colors);
		this.rightGoalieIcon = new PlayerIcon(ColorDB
				.getColor("goalie_r_color"));

		JPanel jp2 = new JPanel();
		jp2.setLayout(new GridBagLayout());
		jp2.add(new JLabel("Ball", this.ballIcon, JLabel.LEFT), gbc1);
		jp2.add(this.ballColor, gbc2);
		jp2.add(new JLabel("Left Player", this.leftPlayerIcon, JLabel.LEFT),
				gbc1);
		jp2.add(this.leftPlayerColor, gbc2);
		jp2.add(new JLabel("Left Goalie", this.leftGoalieIcon, JLabel.LEFT),
				gbc1);
		jp2.add(this.leftGoalieColor, gbc2);
		jp2.add(new JLabel("Right Player", this.rightPlayerIcon, JLabel.LEFT),
				gbc1);
		jp2.add(this.rightPlayerColor, gbc2);
		jp2.add(new JLabel("Right Goalie", this.rightGoalieIcon, JLabel.LEFT),
				gbc1);
		jp2.add(this.rightGoalieColor, gbc2);

		tabbedPane.add("Color", jp2);

		/* Publish */
		JPanel jp3 = new JPanel();

		String type[] = javax.imageio.ImageIO.getWriterFormatNames();
		this.typeBox = new JComboBox(type);
		jp3.add(this.typeBox);

		tabbedPane.add("Publish", jp3);

		this.jd.pack();
	}

	public void show() {

		/* GameEvent Tab */
		ScenePlayer sp = this.soccerscope.getScenePlayer();
		for (int i = GameEvent.MIN; i < GameEvent.MAX; i++) {
			this.event[i].setSelected(sp.getEventEnabled(i));
			this.eventOffset[i].setText(String.valueOf(sp.getEventOffset(i)));
		}

		/* Color */
		if (Color2.getColorName(ColorDB.getColor("ball")) != null)
			this.ballColor.setSelectedItem(Color2.getColorName(ColorDB
					.getColor("ball")));
		else
			this.ballColor.setSelectedItem("custom");
		if (Color2.getColorName(ColorDB.getColor("team_l_color")) != null)
			this.leftPlayerColor.setSelectedItem(Color2.getColorName(ColorDB
					.getColor("team_l_color")));
		else
			this.leftPlayerColor.setSelectedItem("custom");
		if (Color2.getColorName(ColorDB.getColor("goalie_l_color")) != null)
			this.leftGoalieColor.setSelectedItem(Color2.getColorName(ColorDB
					.getColor("goalie_l_color")));
		else
			this.leftGoalieColor.setSelectedItem("custom");
		if (Color2.getColorName(ColorDB.getColor("team_r_color")) != null)
			this.rightPlayerColor.setSelectedItem(Color2.getColorName(ColorDB
					.getColor("team_r_color")));
		else
			this.rightPlayerColor.setSelectedItem("custom");
		if (Color2.getColorName(ColorDB.getColor("goalie_r_color")) != null)
			this.rightGoalieColor.setSelectedItem(Color2.getColorName(ColorDB
					.getColor("goalie_r_color")));
		else
			this.rightGoalieColor.setSelectedItem("custom");
		this.ballColor.addItemListener(this);
		this.leftPlayerColor.addItemListener(this);
		this.leftGoalieColor.addItemListener(this);
		this.rightPlayerColor.addItemListener(this);
		this.rightGoalieColor.addItemListener(this);

		/* Publish */
		Preferences pf = SoccerScopePreferences.getPreferences();
		String ptype = pf.get("PublishType", "png");
		if (ptype != null)
			this.typeBox.setSelectedItem(ptype);

		this.jd.setLocationRelativeTo(null);
		this.jd.setVisible(true);
	}

	public void actionPerformed(ActionEvent ae) {
		Object o = ae.getSource();

		if (o == this.cancel) {
			this.jd.dispose();
			return;
		}

		Preferences pf = SoccerScopePreferences.getPreferences();

		/* GameEvent Tab */
		ScenePlayer sp = this.soccerscope.getScenePlayer();
		for (int i = GameEvent.MIN; i < GameEvent.MAX; i++) {
			sp.setEventEnabled(i, this.event[i].isSelected());
			try {
				int offset = Integer.parseInt(this.eventOffset[i].getText());
				sp.setEventOffset(i, offset);
			} catch (NumberFormatException nfe) {
				System.err.println("invalid integer: "
						+ GameEvent.getEventName(i) + "Offset("
						+ this.eventOffset[i].getText() + ")");
			}
		}

		/* Color */
		ColorDB.putColor("ball", this.ballIcon.getColor());
		ColorDB.putColor("team_l_color", this.leftPlayerIcon.getColor());
		ColorDB.putColor("goalie_l_color", this.leftGoalieIcon.getColor());
		ColorDB.putColor("team_r_color", this.rightPlayerIcon.getColor());
		ColorDB.putColor("goalie_r_color", this.rightGoalieIcon.getColor());

		/* Publish */
		pf.put("PublishType", (String) this.typeBox.getSelectedItem());

		this.jd.dispose();
	}

	public void itemStateChanged(ItemEvent ie) {
		if (ie.getStateChange() != ItemEvent.SELECTED)
			return;

		Object o = ie.getSource();

		String colorname = (String) ((JComboBox) o).getSelectedItem();
		Color c;
		if (colorname == "custom") {
			c = JColorChooser.showDialog(null, "Select Color", Color.white);
		} else {
			c = Color2.getColor(colorname);
		}
		if (o == this.ballColor) {
			this.ballIcon.setColor(c);

		} else if (o == this.leftPlayerColor) {
			this.leftPlayerIcon.setColor(c);

		} else if (o == this.leftGoalieColor) {
			this.leftGoalieIcon.setColor(c);

		} else if (o == this.rightPlayerColor) {
			this.rightPlayerIcon.setColor(c);

		} else if (o == this.rightGoalieColor) {
			this.rightGoalieIcon.setColor(c);

		}
		this.jd.repaint();
	}
}
/**
 * $Log: PreferenceDialog.java,v $
 * Revision 1.1.28.4  2006/11/23 09:41:05  endert
 * Added License Note at top of each *.java file.
 *
 * Revision 1.1.28.3  2006/11/13 16:50:19  endert
 * Removed CVS-Logs and author tags
 *
 * Revision 1.1.28.2  2006/07/31 13:36:27  endert
 * Added Java-Doc to constructors if missing, minor refactorings.
 * Revision 1.1.28.1 2006/07/26 15:28:28 endert
 * Formatted Code Revision 1.1 2006/03/13 07:37:52 axle moved from ROOT/src for
 * Maven 2 Buildsystem and Team/Soccerscope separation
 * 
 * Revision 1.3 2006/03/07 12:46:36 endert Merge after refactoring: - New
 * State-Interface: One Method for each action type - Action Bean removed -
 * ActionFactorys for helper methods, States calculate main-Action themselves -
 * formatted code - added State-List in Monitor (Context-Menu of Player) - other
 * smaller stuff
 * 
 * Revision 1.2.2.1 2006/02/25 22:07:31 wetzker formated
 * 
 * Revision 1.2 2006/02/22 16:04:48 endert This is done: - Rebound SoccerScope
 * to Agents (can be added or removed in config-file) - Situations now all
 * extend abstract class Situation - Refactorings: ActionBean _ removed from
 * Attributes - SoccerScope will automatically connect to host given in
 * Start-Call - Trainer uses the same host as soccerscope - Removed unused stuff
 * (old confs, bat, etc.) - Code formatted (good luck when merging) - organized
 * imports - all that i forgot to mention here Revision 1.1 2005/04/20 10:39:05
 * endert Fresh Team Repository installation from /home/e/endert/Robocup to
 * here.
 * 
 * Revision 1.5 2005/03/20 21:39:36 andpaul removed some warnings
 * 
 * 
 * 
 * 
 */
